package org.elasticsearch.index.analysis;

import org.apache.lucene.analysis.Analyzer;
import org.apdplat.word.lucene.ChineseWordAnalyzer;
import org.apdplat.word.segmentation.Segmentation;
import org.apdplat.word.segmentation.SegmentationAlgorithm;
import org.apdplat.word.segmentation.SegmentationFactory;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment;
import org.elasticsearch.index.IndexSettings;

/**
 * Created with IntelliJ IDEA.
 * User: Kenn
 * Date: 2017/9/1
 * Time: 下午2:52
 * Description:
 */
public class ChineseWordAnalyzerProvider extends AbstractIndexAnalyzerProvider<Analyzer> {

    private final ChineseWordAnalyzer analyzer;

    public ChineseWordAnalyzerProvider(IndexSettings indexSettings, Environment env, String name, Settings settings, SegmentationAlgorithm algorithm) {
        super(indexSettings, name, settings);
        Segmentation segmentation = SegmentationFactory.getSegmentation(algorithm);
        this.analyzer = new ChineseWordAnalyzer(segmentation);
    }

    public static ChineseWordAnalyzerProvider getMaximumMatchingAnalyzerProvider(IndexSettings indexSettings, Environment env, String name, Settings settings) {
        return new ChineseWordAnalyzerProvider(indexSettings, env, name, settings, SegmentationAlgorithm.MaximumMatching);
    }

    public static ChineseWordAnalyzerProvider getReverseMaximumMatchingAnalyzerProvider(IndexSettings indexSettings, Environment env, String name, Settings settings) {
        return new ChineseWordAnalyzerProvider(indexSettings, env, name, settings, SegmentationAlgorithm.ReverseMaximumMatching);
    }

    public static ChineseWordAnalyzerProvider getMinimumMatchingAnalyzerProvider(IndexSettings indexSettings, Environment env, String name, Settings settings) {
        return new ChineseWordAnalyzerProvider(indexSettings, env, name, settings, SegmentationAlgorithm.MinimumMatching);
    }

    public static ChineseWordAnalyzerProvider getReverseMinimumMatchingAnalyzerProvider(IndexSettings indexSettings, Environment env, String name, Settings settings) {
        return new ChineseWordAnalyzerProvider(indexSettings, env, name, settings, SegmentationAlgorithm.ReverseMinimumMatching);
    }

    public static ChineseWordAnalyzerProvider getBidirectionalMaximumMatchingAnalyzerProvider(IndexSettings indexSettings, Environment env, String name, Settings settings) {
        return new ChineseWordAnalyzerProvider(indexSettings, env, name, settings, SegmentationAlgorithm.BidirectionalMaximumMatching);
    }

    public static ChineseWordAnalyzerProvider getBidirectionalMinimumMatchingAnalyzerProvider(IndexSettings indexSettings, Environment env, String name, Settings settings) {
        return new ChineseWordAnalyzerProvider(indexSettings, env, name, settings, SegmentationAlgorithm.BidirectionalMinimumMatching);
    }

    public static ChineseWordAnalyzerProvider getBidirectionalMaximumMinimumMatchingAnalyzerProvider(IndexSettings indexSettings, Environment env, String name, Settings settings) {
        return new ChineseWordAnalyzerProvider(indexSettings, env, name, settings, SegmentationAlgorithm.BidirectionalMaximumMinimumMatching);
    }

    public static ChineseWordAnalyzerProvider getFullSegmentationAnalyzerProvider(IndexSettings indexSettings, Environment env, String name, Settings settings) {
        return new ChineseWordAnalyzerProvider(indexSettings, env, name, settings, SegmentationAlgorithm.FullSegmentation);
    }

    public static ChineseWordAnalyzerProvider getMinimalWordCountAnalyzerProvider(IndexSettings indexSettings, Environment env, String name, Settings settings) {
        return new ChineseWordAnalyzerProvider(indexSettings, env, name, settings, SegmentationAlgorithm.MinimalWordCount);
    }

    public static ChineseWordAnalyzerProvider getMaxNgramScoreAnalyzerProvider(IndexSettings indexSettings, Environment env, String name, Settings settings) {
        return new ChineseWordAnalyzerProvider(indexSettings, env, name, settings, SegmentationAlgorithm.MaxNgramScore);
    }

    public Analyzer get() {
        return analyzer;
    }
}
